package bean;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

public class ManagerBean {
    public ArrayList<Manager>adminArrayList;
    public HashMap<String,String>adminMap;
    public Connection connection;
    public PreparedStatement preparedStatement;
    public Statement statement;
    public ResultSet resultSet;

    public ManagerBean() {
        this.adminArrayList = new ArrayList<>();
        this.adminMap = new HashMap<>();
    }

    public ArrayList<Manager> getManagerArrayList() {
        return adminArrayList;
    }

    public HashMap<String, String> getManagerMap() {
        return adminMap;
    }

    public void link() {
        try {
            Class.forName("org.hsqldb.jdbc.JDBCDriver");
            Connection connection=DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void loadManager(){
        String sql="select * from Manager";
        try {
            statement=connection.createStatement();
            resultSet=statement.executeQuery(sql);
            while(resultSet.next()){
                Manager admin=new Manager(resultSet.getString(1),resultSet.getString(2));
                adminArrayList.add(admin);
                adminMap.put(resultSet.getString(1),resultSet.getString(2));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public Manager selectManager(String id){
        String sql="select * from Manager where id=?";
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,id);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                Manager admin = new Manager(resultSet.getString(1),resultSet.getString(2));
                return admin;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
}
